package cn.fl.ref.account.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.account.service.IFncGroupCompanyAccountService;
import cn.fl.ref.account.service.IFncGroupCompanyService;
import cn.fl.ref.account.vo.FncGroupCompanyAccountVO;
import cn.fl.ref.account.vo.FncGroupCompanyVO;
import cn.fl.system.constant.SysLogConstant;
import fl.ref.account.condition.FncGroupCompanyAccountQueryCondition;
import fl.ref.account.condition.FncGroupCompanyQueryCondition;
import fl.ref.account.constant.FncGroupCompanyConstants;

/**
 * @Description: 集团公司 控制层
 */
@Controller
@RequestMapping("/account/fncGroupCompany")
public class FncGroupCompanyController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(FncGroupCompanyController.class);

	@Autowired
	private IFncGroupCompanyService fncGroupCompanyService;
	@Autowired
	private IFncGroupCompanyAccountService fncGroupCompanyAccountService;

	/**
	 * 跳转框架页面
	 */
	@RequestMapping("/toListPageFrame.do")
	public String toListPageFrame(HttpServletRequest request) {

		return "rfbase/account/fncGroupCompanyFrame";
	}

	/**
	 * 跳转到列表页面
	 */
	@RequestMapping("/toListPage.do")
	public String toListPage(HttpServletRequest request) {

		return "rfbase/account/fncGroupCompanyList";
	}

	/**
	 * 列表页面查询
	 */
	@RequestMapping("/initList.do")
	@ResponseBody
	public Object initList(FncGroupCompanyQueryCondition qc, HttpServletRequest request) {

		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		qc.setIsDel(FncGroupCompanyConstants.IS_DEL_NO);
		page.setCondition(qc);

		Pager pager = this.fncGroupCompanyService.searchListPage(page);
		ListPageVO<FncGroupCompanyVO> listPage = new ListPageVO<FncGroupCompanyVO>(
				(List<FncGroupCompanyVO>) pager.getResultList(), pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page),
				CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 集团公司新增或修改
	 */
	@RequestMapping(value = "/save.do")
	@ResponseBody
	public Object save(FncGroupCompanyVO fncGroupCompanyVO, HttpServletRequest request) {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		try {
			// 验证
			if (StringUtils.isBlank(fncGroupCompanyVO.getName())) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("请填写集团公司名称");
				return result;
			}

			FncGroupCompanyQueryCondition qc = new FncGroupCompanyQueryCondition();
			qc.setName(fncGroupCompanyVO.getName());
			
			List<FncGroupCompanyVO> listFncGroupCompanyVO = fncGroupCompanyService.selectList(qc);
			
			if (fncGroupCompanyVO.getId() != null) {
				// 修改
				FncGroupCompanyVO newFncGroupCompanyVO = fncGroupCompanyService.selectById(fncGroupCompanyVO.getId());
				if (newFncGroupCompanyVO == null) {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("集团公司信息不存在");
					return result;
				}

				if (CollectionUtils.isNotEmpty(listFncGroupCompanyVO) && listFncGroupCompanyVO.size() > 0 
						&& !listFncGroupCompanyVO.get(0).getId().equals(fncGroupCompanyVO.getId())) {
					
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("集团公司名称重复");
					return result;
				}
				
				newFncGroupCompanyVO.setName(fncGroupCompanyVO.getName());
				newFncGroupCompanyVO.setMdfTm(new Date());
				newFncGroupCompanyVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());

				fncGroupCompanyService.update(newFncGroupCompanyVO);
			} else {
				
				if (CollectionUtils.isNotEmpty(listFncGroupCompanyVO) 
						&& listFncGroupCompanyVO.size() > 0) {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("集团公司名称重复");
					return result;
				}
				
				// 新增
				fncGroupCompanyVO.setIsValid(FncGroupCompanyConstants.IS_VALID_YES);
				fncGroupCompanyVO.setIsDel(FncGroupCompanyConstants.IS_DEL_NO);
				fncGroupCompanyVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				fncGroupCompanyVO.setCrtTm(new Date());

				fncGroupCompanyService.add(fncGroupCompanyVO);
			}

		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, null, "操作数据失败.DATA:" + ex.getMessage(),
					CurrentThreadContext.getCurrentUserName(), ex);
		}
		return result;
	}

	/**
	 * 查询集团公司
	 */
	@RequestMapping("/selectById.do")
	@ResponseBody
	public Object selectById(Long id) {
		if (id != null) {
			return fncGroupCompanyService.selectById(id);
		}
		return null;
	}

	/**
	 * 删除集团公司
	 */
	@RequestMapping(value = "/removeById.do")
	@ResponseBody
	public Object removeById(Long id) {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		try {
			// 验证
			if (id == null) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("id不能为空");
				return result;
			}

			fncGroupCompanyService.removeById(id);

			// 删除集团下所有账户
			FncGroupCompanyAccountQueryCondition qc = new FncGroupCompanyAccountQueryCondition();
			qc.setGroupCompanyId(id);
			List<FncGroupCompanyAccountVO> fncGroupCompanyAccountVOList = fncGroupCompanyAccountService.selectList(qc);
			if (CollectionUtils.isNotEmpty(fncGroupCompanyAccountVOList)) {
				for (FncGroupCompanyAccountVO fncGroupCompanyAccountVO : fncGroupCompanyAccountVOList) {
					fncGroupCompanyAccountService.removeById(fncGroupCompanyAccountVO.getId());
				}
			}

		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, null, "操作数据失败.DATA:" + ex.getMessage(),
					CurrentThreadContext.getCurrentUserName(), ex);
		}
		return result;
	}

}
